package com.zsx.shopping.auth.service.impl;

import com.zsx.shopping.auth.converter.AuthConverter;
import com.zsx.shopping.auth.form.RegisterForm;
import com.zsx.shopping.auth.service.RegisterService;
import com.zsx.shopping.auth.service.VerifyService;
import com.zsx.shopping.common.enums.CodeMsg;
import com.zsx.shopping.common.api.R;
import com.zsx.shopping.common.api.user.dto.RegisterDto;
import com.zsx.shopping.common.api.user.feign.UserFeignService;
import com.zsx.shopping.common.exception.ShoppingException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Resource
    private UserFeignService userFeignService;

    @Resource
    private VerifyService verifyService;

    @Resource
    private AuthConverter authConverter;

    @Override
    public void register(RegisterForm registerForm) {
        verifyService.checkVerifyCode(registerForm.getCode());
        RegisterDto registerDto = authConverter.toRegisterDto(registerForm);
        R<Void> r = userFeignService.register(registerDto);
        if (!r.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            throw new ShoppingException(r.getCode(), r.getMsg());
        }
    }
}
